package com.ruoyi.web.controller.h5;

import java.sql.Date;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.H5User;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.user.UserException;
import com.ruoyi.web.domain.UserOrder;
import com.ruoyi.web.domain.UserOrderDetail;
import com.ruoyi.web.service.IUserOrderDetailService;
import com.ruoyi.web.service.IUserOrderService;
import com.ruoyi.web.service.IUserSettlementService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 销售单Controller
 * 
 * @author ruoyi
 * @date 2025-06-11
 */
@RestController
@RequestMapping("/h5/order")
public class H5UserOrderController extends BaseController
{
    @Autowired
    private IUserOrderService userOrderService;

    @Autowired
    private IUserOrderDetailService userOrderDetailService;

    @Autowired
    private IUserSettlementService userSettlementService;

    @GetMapping("/list")
    public TableDataInfo list(UserOrder userOrder,@RequestParam(required = false,defaultValue = "0") Integer type )
    {
        if(type == 0){
            startPage();
        }
        List<UserOrder> list = userOrderService.selectUserOrderList(userOrder);
        if(list.size()>0){
            Long[] ids =list.stream().map(UserOrder::getId).toArray(Long[]::new);
            List<UserOrderDetail> detailList = userOrderDetailService.selectUserOrderDetailByOrderIds(ids);
            list.forEach(item -> {
                item.setDetailList(detailList.stream().filter(detail -> detail.getOrderId().equals(item.getId())).collect(Collectors.toList()));
            });
        }
        
        
        return getDataTable(list);
    }

    /**
     * 查询销售单列表
     */
    @GetMapping("getList")
    public TableDataInfo getList(UserOrder userOrder){
        startPage();
        List<UserOrder> list = userOrderService.selectListByCompanyIdAndMerchantId(userOrder.getCompanyId(),userOrder.getMerchantId());
        return getDataTable(list);
    }
    @GetMapping("getOne")
    public AjaxResult getOne(Long id){

        return AjaxResult.success(userOrderService.selectUserOrderById(id));
    }

    /**
     * 新增销售单
     */
    @Log(title = "销售单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserOrder userOrder)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        H5User h5User= loginUser.getH5User();
        userOrder.setCreator(h5User.getId()); 
        if(h5User.getTop().equals(0L)){
            userOrder.setCompanyId(h5User.getId());
        }else{
            userOrder.setCompanyId(h5User.getTop());
        }
        
        return AjaxResult.success(userOrderService.insertUserOrderReturnUserOrder(userOrder));
    }

    /**
     * 修改销售单
     */
    @Log(title = "销售单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserOrder userOrder)
    {
        
        return AjaxResult.success(userOrderService.updateUserOrderReturnUserOrder(userOrder));
    }

    /**
     * 删除销售单
     */
    @Log(title = "销售单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        UserOrder userOrder = userOrderService.selectUserOrderById(id);
        if(!userOrder.getIsSettlement().equals("0")){
            throw new UserException("order.status.settlement",null);
        }
        int rows = userOrderService.deleteUserOrderById(id);
        if(rows==0){
            throw new UserException("order.delete.fail", null);
        }
        int rows2 = userOrderDetailService.deleteUserOrderDetailByOrderId(id);

        return toAjax(rows+rows2);
    }
}
